package com.lanyun.carworld.controller.user;


import com.lanyun.carworld.common.BaseResponse;
import com.lanyun.carworld.common.ErrorCode;
import com.lanyun.carworld.common.ResultUtils;
import com.lanyun.carworld.context.BaseContext;
import com.lanyun.carworld.exception.BusinessException;
import com.lanyun.carworld.model.dto.user.PostCreateRequest;
import com.lanyun.carworld.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user/post")
@Api(tags = "帖子接口")
@Slf4j
public class PostController {
    @Resource
    private PostService postService;


    /**
     * 创建帖子
     * @param request
     */
    @PostMapping("/create")
    @ApiOperation("创建帖子")
    public BaseResponse<Long> create(PostCreateRequest request) {
        log.info("创建帖子：{}",request);

        //判断帖子内容和标题是否为空post
        if (request.getContent() == null || request.getContent().isEmpty() ||
                request.getTitle() == null || request.getTitle().isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        Long userId = BaseContext.getCurrentId();
        if (userId == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }


        Long postId = postService.create(userId,request);

        return ResultUtils.success(postId);
    }




}
